home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2001 May / SGI IRIX 6.5 Applications 2001 May.iso / dev / java_dev.idb / usr / demos / java / Bounce / Actor.java.z / Actor.java
Encoding:
Java Source  |  2000-05-20  |  2.9 KB  |  130 lines

  1. /*
  2.  
  3. Actor.java
  4.  
  5. The Actor class controls the movement of an actor (ball) and playing
  6. sounds when it hits a wall.
  7.  
  8. */
  9.  
  10. import java.awt.*;
  11. import java.lang.*;
  12. import java.applet.*;
  13.  
  14.  
  15. public class Actor implements Runnable
  16. {
  17.     private String       name;
  18.     private Color        color;
  19.  
  20.     private Stage        stage;
  21.  
  22.     private Thread       runner;
  23.  
  24.     private double       x0,  y0;    // Initial position in each dimension.
  25.     private double       v0x, v0y;   // Initial velocity in each dimension.
  26.     private double       ax,  ay;    // Acceleration in each dimension.
  27.     private double       x,   y;     // Current position in each dimension.
  28.  
  29.     private double       t;
  30.     private final double dt = 0.05;
  31.  
  32.     private final int    r  = 10;
  33.  
  34.  
  35.     public Actor(String n, Color c, Stage s)
  36.     {
  37.         name = n;
  38.         color = c;
  39.         stage = s;
  40.  
  41.         // Initial conditions.
  42.         t = 0;
  43.         x = x0 = 2*r;   y = y0 = 2*r;
  44.         v0x    = 2;     v0y    = 0;      // Velocity of 2 to the right.
  45.         ax     = 0;     ay     = 0.1;    // Acceleration downward.
  46.  
  47.         runner = null;
  48.     }
  49.  
  50.  
  51.     // Methods for stage object to call.
  52.  
  53.     public void start()
  54.     {
  55.         if (runner == null) {
  56.             runner = new Thread(this, name);
  57.             runner.start();
  58.         } else {
  59.             runner.resume();
  60.         }
  61.     }
  62.  
  63.     public void stop() {
  64.         runner.suspend();
  65.     }
  66.  
  67.  
  68.     // Methods for runner thread to run.
  69.  
  70.     public void run()
  71.     {
  72.         while (true) {
  73.             int i;
  74.             for (i=0; i<10; i++) {
  75.                 computeNewPosition();
  76.             }
  77.             try {
  78.                 Thread.sleep(5);
  79.             } catch (InterruptedException e) {
  80.                 break;
  81.             }
  82.         }
  83.     }
  84.  
  85.     public void paint(Graphics g)
  86.     {
  87.         int size = 2 * r;
  88.         g.setColor(color);
  89.         g.fillOval((int)(x - r), (int)(y - r), size, size);
  90.     }
  91.  
  92.  
  93.     // Private methods.
  94.  
  95.     private void computeNewPosition()
  96.     {
  97.         boolean collisionX, collisionY;
  98.  
  99.         // Update t and calculate one-dimensional motion with constant
  100.         // acceleration in each dimension.
  101.         t += dt;
  102.         x = x0 + v0x*t + 0.5*ax*t*t;
  103.         y = y0 + v0y*t + 0.5*ay*t*t;
  104.  
  105.         // Detect collisions with walls.
  106.         collisionX = ((x - r) <= 0 || (x + r) >= stage.getSize().width);
  107.         collisionY = ((y - r) <= 0 || (y + r) >= stage.getSize().height);
  108.  
  109.         if (collisionX || collisionY) {
  110.             // A collision occurred, so set initial conditions to
  111.             // current conditions and reset t.  Reverse velocity in
  112.             // any dimension in which a collision occurred.
  113.             v0x = v0x + ax * t;
  114.             v0y = v0y + ay * t;
  115.             if (collisionX)  v0x = -v0x;
  116.             if (collisionY)  v0y = -v0y;
  117.             x0 = x;
  118.             y0 = y;
  119.             t = 0;
  120.             gong();
  121.         }
  122.     }
  123.  
  124.     private void gong()
  125.     {
  126.          stage.gong();
  127.     }
  128. }
  129.  
  130.